"use strict"


function onLogin() {
  Refresh();
}


async function Refresh() {
  //ShowAssets();
  ShowRunes();
  ShowNFT && ShowNFT();
  BalanceOf();
  ShowMyNFT && ShowMyNFT();
}

// let AssetsAll  = null;
// async function ShowAssets()
// {
//     $.ajax({
//         url: Helper.GetServerIP() + "/Assets",
//                   dataType: "text",
//                   type: "post",
//                   data: { address: Helper.addressCur, offset: 0,count: 12 },
//                   success: function (data) {

//                         // // 删除之前的数据
//                         // var pane_list = $(".tokens-list")[0];
//                         // for (var i = pane_list.children.length - 1; i >= 0; i--) {
//                         //     pane_list.children[i].remove();
//                         // }

//                         // RunesAll = JSON.parse(data);

//                         // for(var ii=0;ii<RunesAll.length;ii++)
//                         // {
//                         //     var item = JSON.parse(RunesAll[ii]);
//                         //     search_insert( pane_list, item.Data, item.Number, item.ID, getDateDiff(item.Created), onclickitem);
//                         // }

//                   },
//                   error: function (err) {
//                     ShowToastrError("Error",err.responseText);
//                   }
//       });

// }


let RunesAll = null;
async function ShowRunes() {
  $.ajax({
    url: Helper.GetServerIP() + "/RunesAll",
    dataType: "text",
    type: "post",
    data: { address: Helper.addressCur, offset: 0, count: 12 },
    success: function (data) {

      // 删除之前的数据
      var pane_list = $(".runes-list")[0];
      for (var i = pane_list.children.length - 1; i >= 0; i--) {
        pane_list.children[i].remove();
      }

      RunesAll = JSON.parse(data);

      for (var ii = 0; ii < RunesAll.length; ii++) {
        var item = JSON.parse(RunesAll[ii]);
        runes_insert(pane_list, item.Data, item.Number, item.ID, getDateDiff(item.Created), onclickitem);
      }

    },
    error: function (err) {
      ShowToastrError("Error", err.responseText);
    }
  });

}

async function ShowNFT() {
  const NFTObj = [
    {
      img: './my_imgae/NFT/01',
      url: 'javascript:onComingsoon();'
    },
    {
      img: './my_imgae/NFT/02',
      url: 'javascript:onComingsoon();'
    },
    {
      img: './my_imgae/NFT/03',
      url: 'javascript:onComingsoon();'
    },
    {
      img: './my_imgae/NFT/04',
      url: 'javascript:onComingsoon();'
    },
    
  ]
  // 删除之前的数据
  // console.log('NFT');
  var pane_list = $(".runes-list")[1];
  // console.log(pane_list);
  for (var i = pane_list.children.length - 1; i >= 0; i--) {
    pane_list.children[i].remove();
  }

  for (let ii = 0; ii < NFTObj.length; ii++) {
    let item = NFTObj[ii];
    NFT_insert(pane_list, item.img, item.url);
  }
}

async function ShowMyNFT() {
  const perCount = 2000*1000;
  if(window._MintsCount === undefined || !window._MintsCount) {
    setTimeout(() => {
      ShowMyNFT();
    }, 200);
    return;
  }
  if( window._MintsCount && window._MintsCount < perCount) {
    console.log(window._MintsCount , "window._MintsCount");
    return;
  }
  const NFTCount = Math.floor(window._MintsCount/perCount);
  console.log(window._MintsCount, 'window._MintsCount2');
  const NFTObj = [
    {
      img: './my_imgae/NFT/01',
      url: 'javascript:onComingsoon();'
    }     
  ]
  // 删除之前的数据
  var pane_list = $(".runes-list")[2];
  for (var i = pane_list.children.length - 1; i >= 0; i--) {
    pane_list.children[i].remove();
  }

  for (let ii = 0; ii < NFTObj.length; ii++) {
    let item = NFTObj[ii];
    for(let j = 0 ; j < NFTCount ; j++ ) {
      NFT_insert(pane_list, item.img, item.url);
    }
  }
}


async function bodyNavClick(pane) {
  console.log(pane, 'pane');
  if (pane === "NFTS") {
    console.log('NFTS');
    Tokens.style.display = 'none';
    Runes.style.display = 'none';
    document.querySelector("#My-NFT").style.display = 'none';
  } else {
    NFTS.style.display = "none";
  }
  var panechild = $("#AssetsTabs .active");
  for (var ii = 0; ii < panechild.length; ii++) {
    $(panechild[ii]).removeClass("active");
  }

  var panechild = $(".tab-content .active");
  for (var ii = 0; ii < panechild.length; ii++) {
    $(panechild[ii]).removeClass("show");
    $(panechild[ii]).removeClass("active");
  }

  $("#AssetsTabs #" + pane).addClass("active");

  $(".tab-content #" + pane + "-pane").addClass("show");
  $(".tab-content #" + pane + "-pane").addClass("active");

  curr_page_index = 0;
  // ShowTabPaneGame();
}


let curr_page_index = 0;
function onPageitem(e) {
  var index = Number.parseInt($(e).attr("data"));
  if (index != curr_page_index) {
    curr_page_index = index;
    ShowRunes();
  }
}

async function BalanceOf() {
  $.ajax({
    url: Helper.GetServerIP() + "/BalanceOf",
    dataType: "text",
    type: "post",
    data: { address: Helper.addressCur, tick: project_tick },
    success: function (data) {
      window._MintsCount = Number(data);
      $("#BalanceOf").html(new BigNumber(data).toFormat());
      $("#BalanceOf2").html(Helper.Div(data, "1000") + " mints");

    },
    error: function (err) {
      ShowToastrError("Error", err.responseText);
    }
  });

}

(function ($) {
  "use strict"



})(jQuery);


